
<template>
  <el-dialog
    :close-on-click-modal="false"
    title="建设单位编辑"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <div class="com-form-title">标识信息</div>
      <el-row>
        <el-col :span="12">
          <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode" label-width="150px">
            <el-input v-model="form.unifiedSocialCreditCode" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="com-form-title">基本信息</div>
      <el-row>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="单位编号" prop="number">
                <el-input v-model="form.number" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单位名称" prop="name">
                <el-input v-model="form.name" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="省份" prop="province">
                <el-select v-model="form.province" :disabled="disableForm" style="width: 100%;" placeholder="请选择省份" @change="provinceChange">
                  <el-option v-for="item in provinceData" :key="item.name" :label="item.name" :value="item.name" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="城市" prop="city">
                <el-select v-model="form.city" :disabled="disableForm" style="width: 100%;" placeholder="请选择城市">
                  <el-option v-for="item in cityData" :key="item.name" :label="item.name" :value="item.name" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="简介" prop="description">
            <el-input v-model="form.description" :disabled="disableForm" type="textarea" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="法人代表" prop="legalPerson">
                <el-input v-model="form.legalPerson" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="注册资金" prop="registeredCapital">
                <el-input v-model="form.registeredCapital" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="com-form-title">扩展信息</div>
      <el-row>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="联系人姓名" prop="contactName">
                <el-input v-model="form.contactName" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人手机" prop="contactPhone">
                <el-input v-model="form.contactPhone" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="固话" prop="fixed">
                <el-input v-model="form.fixed" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮编" prop="postCode">
                <el-input v-model="form.postCode" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" :disabled="disableForm" />
          </el-form-item>
          <el-form-item label="通讯地址" prop="contactAddress">
            <el-input v-model="form.contactAddress" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="单位性质" prop="nature">
                <el-select v-model="form.nature" :disabled="disableForm" style="width: 100%;" placeholder="请选择单位性质">
                  <el-option v-for="item in unitType" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="评定级别" prop="level">
                <el-input v-model="form.level" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="行业类别" prop="industry">
                <el-select v-model="form.industry" :disabled="disableForm" style="width: 100%;" placeholder="请选择单位性质">
                  <el-option v-for="item in industryType" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="传真" prop="fax">
                <el-input v-model="form.fax" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="资金状况" prop="fundingStatus">
            <el-input v-model="form.fundingStatus" :disabled="disableForm" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form-item label="文件上传">
        <uploader v-model="fileList" :disabled="disableForm" :can-download="optType==='detail'" />
      </el-form-item> -->
      <div class="com-form-title">附件</div>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addConstructionUnit, updateConstructionUnit, getConstructionUnitDetail } from '@/api/travellingTraderManage'
import { getAreaList } from '@/api/common'
import { industryType, unitType } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'DirManage',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      industryType,
      unitType,
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        name: '',
        unifiedSocialCreditCode: '',
        number: '',
        province: '',
        city: '',
        legalPerson: '',
        registeredCapital: '',
        address: '',
        description: '',
        contactName: '',
        contactPhone: '',
        fixed: '',
        postCode: '',
        email: '',
        contactAddress: '',
        nature: '',
        level: '',
        industry: '',
        fax: '',
        fundingStatus: ''
      },
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            this.form[key] = this.data[key]
          })
        })
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getConstructionUnitDetail({ constructionUnitId: id }).then(res => {
        const { lstAttachment } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            constructionUnit: {
              ...this.form,
              id: this.optType === 'add' ? '' : this.data.id
            },
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addConstructionUnit : updateConstructionUnit
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
